﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;

public partial class Admin_Course_VideoEdit : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindVideoList();
        }

    }

    /// <summary>
    /// 绑定视频信息
    /// </summary>

    private void BindVideoList()
    {
        string str = Request.QueryString["ItemID"];
   

        if (str != null)
        {
            int itemID = Convert.ToInt32(str);
            Proc_CourseItem_SelectInfoByItemIDResult video = VideoInfoBiz.GetVideoInfo(itemID);
            if (video!=null)
            {
               txtItemName.Text=video.ItemName;
               txtItemNum.Text = video.ItemNum;
               txtVideoLink.Text=video.VideoLink;
               txtCourseLength.Text=video.CourseLength;
               txtValidTime.Text = Convert.ToString(video.ValidTime);
               txtStartTime.Text = Convert.ToString(video.StartTime);
               txtIntroduce.Text = video.Introduce;
               courseID.Value=video.CourseID.ToString();

               string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
               if (video.Image == "" || video.Image == null)
               {

               }
               else
               {
                   imgSignature.ImageUrl =imgPath+ video.Image;
                   lblFileName.Text = video.Image;
               }
            }
        }
    
    }


    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {


        if (txtItemName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入视频名称！";
            txtItemName.Focus();
            return;
        }

        if (txtItemNum.Text.Trim() == "")
        {
            lblWarn.Text = "请输入视频编号！";
            txtItemNum.Focus();
            return;
        }

        if (txtVideoLink.Text.Trim() == "")
        {
            lblWarn.Text = "请输入视频链接！";
            txtVideoLink.Focus();
            return;
        }

        if (txtCourseLength.Text.Trim() == "")
        {
            lblWarn.Text = "请输入视频时长！";
            txtCourseLength.Focus();
            return;
        }

        string validTime = txtValidTime.Text.Trim();
        double result;
        bool b = double.TryParse(validTime, out result);
        if (b)
        {
            lblWarn.Text = "";
        }
        else
        {
            lblWarn.Text = "请输入正确的有效期！";
            txtValidTime.Focus();
            return;
        }

        //判断是否为时间格式
        string sTime = txtStartTime.Text.Trim();
        DateTime dtDate;
        if (DateTime.TryParse(sTime, out dtDate))
        {
            txtStartTime.Focus();

        }
        else
        {
            lblWarn.Text = "开始时间格式不对哦！";
            return;
        }


        string id = Request.QueryString["ItemID"];
        int itemID = Convert.ToInt32(id);
        string itemName = txtItemName.Text.Trim();
        string itemNum = txtItemNum.Text.Trim();
        string videoLink = txtVideoLink.Text.Trim();
        string courseLength = txtCourseLength.Text.Trim();
     
        DateTime startTime = Convert.ToDateTime(txtStartTime.Text.Trim());
        string introduce = txtIntroduce.Text.Trim();
        string image = lblFileName.Text;

        CourseItem courseItem = new CourseItem()
        {
            ItemID=itemID,
      //      CourseID = courseID,
            ItemName = itemName,
            ItemNum=itemNum,
            VideoLink = videoLink,
            CourseLength = courseLength,
            ValidTime = Convert.ToDouble(validTime),
            StartTime = startTime,
            Introduce = introduce,
            Image = image
        };


            //编辑视频信息     
            courseItem.ItemID = Convert.ToInt32(id);

            bool d = VideoInfoBiz.VideoUpdate(courseItem);
            if (d)
            {
                lblWarn.Text = "视频信息编辑成功！";
                lblWarn.ForeColor = System.Drawing.Color.Green;
                btnOK.Enabled = false;

            }
            else
            {
                lblWarn.Text = "视频信息编辑失败！";
                lblWarn.ForeColor = System.Drawing.Color.Red;

            }
         //   Proc_CourseItem_SelectInfoByItemIDResult video = VideoInfoBiz.GetVideoInfo(itemID);

        //    Response.Redirect("VideoList.aspx?CourseID=" + video.CourseID.ToString());

            Response.Redirect("VideoList.aspx?CourseID=" + courseID.Value);
     

    }


    /// <summary>
    /// 上传讲师图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFile.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFile.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;
                lblFileName.Text = newName + fileExtension;
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfo.Text = "上传成功";
                imgSignature.ImageUrl = imgPath + newName + fileExtension;
            }
        }
    }




    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("VideoList.aspx?CourseID=" + courseID.Value);
    }




}